﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class EvlOrmDemo_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}


#region 实体类


public partial class Stu
{
    public String TableName = String.Empty;
    public String TableType = String.Empty;
    public Stu()
    {
        TableName = @"TbStu";
        TableType = @"Table";
    }


    protected void SetPropertyName()
    {
        FiledsList = new String[] { "ID", "Name", "Code" };
    }


    public decimal ID
    {
        get { return GetProperty<decimal>("ID"); }
        set { SetProperty("ID", value); }
    }

    public String Name
    {
        get { return GetProperty<String>("Name"); }
        set { SetProperty("Name", value); }
    }

    public String Code
    {
        get { return GetProperty<String>("Code"); }
        set { SetProperty("Code", value); }
    }




    private void SetProperty(string PropertyName, object obj)
    {
        //1.装箱,并且保存值到装箱数组
        //2.设置属性名称到属性改变列表中
        SaveValue(PropertyName, obj);
    }

    private void SaveValue(string PropertyName, object obj)
    {
        for (int i = 0; i < FiledsList.Length; i++)
        {
            if (String.Compare(PropertyName, FiledsList[i]) == 0)
            {
                ChangeList[i] = true;
                FiledsValueArray[i] = obj;
                break;
            }
        }
    }


    protected List<String> GetChangePropertyList()
    {
        List<String> li = new List<String>();
        for (int i = 0; i < ChangeList.Length; i++)
        {
            if (ChangeList[i])
            {
                li.Add(FiledsList[i]);
            }
        }
        return li;
    }


    private bool[] ChangeList;

    //定义字段列表
    private String[] _filedsList;
    public String[] FiledsList
    {
        get
        {
            if (_filedsList == null)
            {
                SetPropertyName();
                ChangeList = new bool[FiledsList.Length];
            }
            return _filedsList;
        }
        set { _filedsList = value; }
    }



    //定义值装箱数组
    private object[] _filedsValueArray;
    public object[] FiledsValueArray
    {
        get
        {
            if (_filedsValueArray == null)
            {
                _filedsValueArray = new object[FiledsList.Length];
            }
            return _filedsValueArray;
        }
        set
        {
            _filedsValueArray = value;
        }
    }

    private T GetProperty<T>(string PropertyName)
    {
        //添加属性变化事件
        OnpropertyGetting(PropertyName);
        //在装箱列表中获取值
        object obj = GetPropertyValue(PropertyName);
        //值拆箱并且返回
        return ConvertValue<T>(obj);
    }

    private T ConvertValue<T>(object obj)
    {
        if (obj != null)
            return (T)obj;
        else if (obj == DBNull.Value || obj == null)
        {
            if (typeof(T) == typeof(DateTime))
            {
                object o = DateTime.MinValue;
                return (T)o;
            }
            else
                return default(T);
        }
        else
        {
            //如果 Value为 decimal类型，Ｔ　为double 类型， (T)Value 将发生错误
            return (T)Convert.ChangeType(obj, typeof(T));
        }
    }

    private object GetPropertyValue(string PropertyName)
    {
        for (int i = 0; i < FiledsList.Length; i++)
        {
            if (FiledsList[i] == PropertyName)
            {
                return FiledsValueArray[i];
            }
        }
        return new object();
    }

    //属性名称被使用时促发事件
    public event EventHandler<PropertyGettingEventArgs> PropertyGetting;
    public void OnpropertyGetting(string Name)
    {
        if (this.PropertyGetting != null)
        {
            this.PropertyGetting(this,new PropertyGettingEventArgs(Name));
        }
    }
}

#region 事件参数
public partial class PropertyGettingEventArgs : EventArgs
{
    private String _propertyName;
    public String PropertyName
    {
        get { return _propertyName; }
        set { _propertyName = value; }
    }

    public PropertyGettingEventArgs(String Name)
    {
        this.PropertyName = Name;
    }
}
#endregion






#endregion





